﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FlashPay.Service.Impl
{
    using FlashPay.DAO.Interface;
    using FlashPay.EF.Models;
    using FlashPay.Entity;
    using FlashPay.Entity.Enum;
    using FlashPay.Entity.Parameter;
    using FlashPay.Entity.Request.SysAnnouncement;
    using FlashPay.Entity.Response.SysAnnouncement;
    using FlashPay.Service.Interface;

    /// <summary>
    /// 公告型业务接口
    /// </summary>
    public class SysAnnouncementServiceImpl: SysAnnouncementService
    {
        private readonly SysAnnouncementDao _sysAnnouncementDao;

        public SysAnnouncementServiceImpl(SysAnnouncementDao sysAnnouncementDao)
        {
            this._sysAnnouncementDao = sysAnnouncementDao;
        }

        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <returns></returns>
        public JResult<SysAnnouncementResponse> Get(int id)
        {
            return _sysAnnouncementDao.Get(id);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(SysAnnouncement model)
        {
            return _sysAnnouncementDao.Add(model);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        public bool Update(SysAnnouncement model)
        {
            return _sysAnnouncementDao.Update(model);
        }

        /// <summary>
        /// 公告保存或修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JResult SaveOrEdit(SaveOrEditSysAnnouncementRequest request)
        {
            var result = new JResult() {
                Success = false
            };
            try
            {
                if (string.IsNullOrEmpty(request.Subject))
                {
                    throw new Exception("标题不能为空！");
                }

                if (string.IsNullOrEmpty(request.Content))
                {
                    throw new Exception("内容不能为空！");
                }

                if (string.IsNullOrEmpty(request.Sdate))
                {
                    throw new Exception("开始时间不能为空！");
                }

                if (string.IsNullOrEmpty(request.Edate))
                {
                    throw new Exception("结束时间不能为空！");
                }

                var popups = new List<sbyte>()
                {
                    (sbyte)Popups.弹窗,
                    (sbyte)Popups.不弹窗
                };

                if (!popups.Contains(request.Popups))
                {
                    throw new Exception("请选择弹窗提示！");
                }

                if (request.AnnouncementId > 0)
                {
                    var model = _sysAnnouncementDao.Get(request.AnnouncementId);
                    if (model.Data == null)
                    {
                        throw new Exception("公告记录不存在！");
                    }

                    var sysAnnouncement = new SysAnnouncement()
                    {
                        AnnouncementId = model.Data.AnnouncementId,
                        Popups = request.Popups,
                        Subject = request.Subject,
                        Content = request.Content,
                        Sdate = Convert.ToDateTime(request.Sdate),
                        Edate = Convert.ToDateTime(request.Edate),
                    };

                    if (request.Popups == (sbyte)Popups.弹窗)
                    {
                        if (!_sysAnnouncementDao.UpdatePopups(model.Data.AnnouncementId))
                        {
                            throw new Exception("操作失败！");
                        }
                    }
                    if (_sysAnnouncementDao.Update(sysAnnouncement))
                    {
                        result.Success = true;
                        result.SuccessMessage = "操作成功！";
                    }
                    else
                    {
                        result.Success = false;
                        result.SuccessMessage = "操作失败！";
                    }
                }
                else
                {
                    var sysAnnouncement = new SysAnnouncement()
                    {
                        Popups = request.Popups,
                        Subject = request.Subject,
                        Content = request.Content,
                        Sdate = Convert.ToDateTime(request.Sdate),
                        Edate = Convert.ToDateTime(request.Edate),
                        CreateId = request.CreateId,
                        CreateDate = DateTime.Now
                    };

                    if (request.Popups == (sbyte)Popups.弹窗)
                    {
                        if (!_sysAnnouncementDao.UpdatePopups(0)) {
                            throw new Exception("操作失败！");
                        }
                    }

                    if (_sysAnnouncementDao.Add(sysAnnouncement)>0)
                    {
                        result.Success = true;
                        result.SuccessMessage = "操作成功！";
                    }
                    else
                    {
                        result.Success = false;
                        result.SuccessMessage = "操作失败！";
                    }
                }
            }
            catch (Exception ex) {
                result.Success = false;
                result.ErrorMessage = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">系统编号</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            return _sysAnnouncementDao.Delete(id);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id">系统编号</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public bool UpdateStatus(int id, sbyte status)
        {
            return _sysAnnouncementDao.UpdateStatus(id, status);
        }

        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <returns></returns>
        public JResult<List<SysAnnouncementResponse>> GetList(SysAnnouncementQuery query)
        {
            return _sysAnnouncementDao.GetList(query);
        }
    }
}
